<?php
// $Id: include/functions.php,v 1.11 2004/12/02 C.Felix AKA the Cat
//  ------------------------------------------------------------------------ //
//             XF Guestbook                                                  //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //
function xfgb_getCountry($criteria = null, $limit=0, $start=0) {
	global $xoopsDB, $action;
	
	$ret = array();

	$sql = "SELECT * FROM ".$xoopsDB->prefix("xfguestbook_country"); 
	if (isset($criteria) && $criteria !='') {
		$sql .= ' WHERE '.$criteria;
	}
	$sql .= " ORDER BY country_code ASC";
	$result = $xoopsDB->query($sql,$limit,$start);
	while ( $myrow = $xoopsDB->fetchArray($result) ) {
		array_push($ret,$myrow);
	}
	return $ret;
}

function xfgb_getmsg($msg){
global $nb_perpage, $xoopsTpl, $xoopsConfig;

	$msg_count = count($msg);
	$max = ( $msg_count < $nb_perpage ) ? $msg_count : $nb_perpage;
	$xoopsTpl->assign('msg_lang_name', $xoopsConfig['language']);
	foreach( $msg as $onemsg ){			
		xfgb_assigntpl($onemsg);	
	}
}

function xfgb_assigntpl($onemsg){
global $nbmsg, $xoopsModule, $xoopsTpl;

	 	if ($poster = xfgb_get_user_data($onemsg->getVar('user_id'))) {
			$a_msg = &$poster;
 		} else {
 			$a_msg = array();
 			$a_msg['poster'] = $onemsg->getVar('uname');
			$a_msg['rank'] = '';
			$a_msg['rank_img'] = '';
			$a_msg['avatar'] = '';
		}
		// email
		if ($onemsg->getVar('email')) 
			$a_msg['email'] = "<a href=\"javascript:openWithSelfMain('".XOOPS_URL."/modules/xfguestbook/contact.php?msg_id=".$onemsg->getVar('msg_id')."', 'contact', 600, 450);\"><img src=\"".XOOPS_URL."/images/icons/email.gif\" alt=\""._SENDEMAILTO."\" /></a>";

		// url
		if ($onemsg->getVar('url'))
			 $a_msg['url'] = '<a href="'.$onemsg->getVar('url').'" target="_blank"><img src="'.XOOPS_URL.'/images/icons/www.gif" alt="'._VISITWEBSITE.'"></a>';

		// gender
		if ($onemsg->getVar('gender') !='') 
			$a_msg['gender'] = '<a href="index.php?op=show_gender&param='.$onemsg->getVar('gender').'"><img src="images/'.$onemsg->getVar('gender').'.gif"</a>';

		// flag
		$flag = XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/images/flags/".$onemsg->getVar('country').".gif";
		$arr_country = xfgb_getCountry("country_code ='".$onemsg->getVar('country')."'");	
		$country_name = (count($arr_country) > 0) ? $arr_country[0]['country_name'] : '';
		if (file_exists($flag)) {
			$a_msg['country'] = "<img src=\"".XOOPS_URL."/modules/xfguestbook/images/flags/".$onemsg->getVar('country').".gif\" alt=\"".$country_name."\">";
		} else {
			$a_msg['country'] = $country_name;
		}
		$a_msg['country'] = "<a href=\"index.php?op=show_country&param=".$onemsg->getVar('country')."\">".$a_msg['country']."</a>";
		
		$a_msg['msg_id'] = $onemsg->getVar('msg_id');
		$a_msg['i'] = $nbmsg;
		$a_msg['title'] = $onemsg->getVar('title');
		$a_msg['date'] = formatTimestamp($onemsg->getVar('post_time'),"s");
		$a_msg['message'] = $onemsg->getVar('message');
		$a_msg['note_msg'] = $onemsg->getVar('note');
		$a_msg['poster_ip'] = $onemsg->getVar('poster_ip');
		$a_msg['moderate'] = $onemsg->getVar('moderate');
		
		$a_msg['photo'] = $onemsg->getVar('photo');

		$xoopsTpl->append('msg', $a_msg);
		$nbmsg--;
}

function xfgb_get_user_data($user_id){
global $xoopsUser, $xoopsModuleConfig;

	if (!intval($user_id))
		return false;
		
	$poster = new XoopsUser($user_id);
	if ($poster->isActive()) {
		($xoopsUser) ? $a_poster['poster'] = "<a href='../../userinfo.php?uid=$user_id'>".$poster->uname()."</a>" : $a_poster['poster'] = $poster->uname();
		if ($xoopsModuleConfig['display_avatar']) {
			$rank = $poster->rank();
			($rank['title']) ? $a_poster['rank'] = $rank['title'] : $a_poster['rank'] = '';
			($rank['image']) ? $a_poster['rank_img'] = "<img src='".XOOPS_URL."/uploads/".$rank['image']."' alt='' />" : $a_poster['rank_img'] = '';
			($poster->user_avatar()) ? $a_poster['avatar'] = "<img src='".XOOPS_URL."/uploads/".$poster->user_avatar()."' alt='' />" : $a_poster['avatar'] = '';
		} else {
			$a_poster['rank'] = '';
			$a_poster['avatar'] = '';
			$a_poster['rank_img'] = '';
		}
		return $a_poster;
	} else {
		return false;
	}
}

// Effacement fichiers temporaires
function xfgb_clear_tmp_files( $dir_path , $prefix = 'tmp_' ) {

	if( ! ( $dir = @opendir( $dir_path ) ) ) 
		return 0 ;
	$ret = 0 ;
	$prefix_len = strlen( $prefix ) ;
	while( ( $file = readdir( $dir ) ) !== false ) {
		if( strncmp( $file , $prefix , $prefix_len ) === 0 ) {
			if( @unlink( "$dir_path/$file" ) ) $ret ++ ;
		}
	}
	closedir( $dir ) ;
	return $ret ;
}


function xfgb_copyright() {
	global $xoopsTpl, $xoopsOption, $xoopsModule;
	
	include(XOOPS_ROOT_PATH."/modules/".$xoopsModule->dirname()."/xoops_version.php");
	$copyright = _MD_XFGB_COPYRIGHT.$modversion['version'];

	if (isset($xoopsOption['template_main'])) {
		$xoopsTpl->assign('msg_copyright', $copyright);
	}
}

?>